using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Starznet.SharePoint.Controls.Web;

namespace Starznet.SharePoint.SPDAT.ApplicationPages.Settings
{
    public class SiteColumnDevViewPage : StarznetBaseLayoutPage
    {
        protected GroupingRepeater RptColumns;
        protected DropDownList GroupFilter;

        protected override void OnLoad(EventArgs e)
        {
            RptColumns.GroupingColumn = "Group";
            RptColumns.IsGroupingDisabled = false;

            string sFilter = this.GroupFilter.SelectedValue;

            DataView oDv = this.PopulateData().DefaultView;
            oDv.Sort = "Group ASC, DisplayName ASC";

            if (IsPostBack && !String.IsNullOrEmpty(sFilter))
                oDv.RowFilter = "Group = '" + this.GroupFilter.SelectedValue + "'";
        
            if (!IsPostBack) GroupFilter.Items.Insert(0, new ListItem("All Columns", ""));

            RptColumns.DataSource = oDv;
            RptColumns.DataBind();
        }

        private DataTable PopulateData()
        {
            DataTable oDT = new DataTable();
            oDT.Columns.Add(new DataColumn("DisplayName"));
            oDT.Columns.Add(new DataColumn("InternalName"));
            oDT.Columns.Add(new DataColumn("Type"));
            oDT.Columns.Add(new DataColumn("Source"));
            oDT.Columns.Add(new DataColumn("Id"));
            oDT.Columns.Add(new DataColumn("Hidden"));
            oDT.Columns.Add(new DataColumn("SchemaXML"));
            oDT.Columns.Add(new DataColumn("Group"));

            DataRow oRow = null;
            SPField oField = null;
            SPFieldCollection oFields = SPContext.Current.Web.Fields;
            for(int i = 0;i<oFields.Count;i++)
            //foreach (SPField oField in SPContext.Current.Web.Fields)
            {
                try
                {
                    oField = oFields[i];
                    oRow = oDT.NewRow();
                    oRow["DisplayName"] = oField.Title;
                    oRow["InternalName"] = oField.InternalName;
                    oRow["Type"] = oField.Type.ToString();
                    oRow["Id"] = oField.Id.ToString();
                    oRow["Hidden"] = (oField.Hidden) ? "True" : "False";
                    oRow["SchemaXML"] = oField.SchemaXml;
                    oRow["Group"] = oField.Group;

                    oDT.Rows.Add(oRow);

                    if (!IsPostBack)
                    {
                        if (GroupFilter.Items.FindByValue(oField.Group) == null)
                            GroupFilter.Items.Add(new ListItem(oField.Group, oField.Group));
                    }
                }
                catch (KeyNotFoundException oKfX)
                {
                    //Ignore this exception - an invalid field has been encountered
                }
            }
            return oDT;
        }
    }
}